﻿using DevExpress.Map.Kml.Model;
using DevExpress.XtraRichEdit.Model;
using AutoMan.Dtos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMan.Helper
{
    public class TreeListHelper
    {
        /// <summary>
        /// 为树控件设置数据源
        /// </summary>
        /// <param name="treeList">树控件</param>
        /// <param name="data">数据对象</param>
        /// <param name="fieldName">显示的字段</param>
        /// <param name="keyFieldName">键字段</param>
        /// <param name="parentFieldName">父级键字段</param>
        public static void TreeNode(DevExpress.XtraTreeList.TreeList treeList, List<TreeListDto> data, string fieldName, string keyFieldName, string parentFieldName)
        {
            #region 设置列头、节点指示器面板、表格线样式

            treeList.OptionsView.ShowColumns = true;             //隐藏列标头
            treeList.OptionsView.ShowIndicator = true;           //隐藏节点指示器面板

            treeList.OptionsView.ShowHorzLines = false;           //隐藏水平表格线
            treeList.OptionsView.ShowVertLines = false;           //隐藏垂直表格线
            treeList.OptionsView.ShowIndentAsRowStyle = true;

            #endregion

            #region 初始禁用单元格选中，禁用整行选中

            treeList.OptionsView.ShowFocusedFrame = true;                               //设置显示焦点框
            treeList.OptionsSelection.EnableAppearanceFocusedCell = false;              //禁用单元格选中
            treeList.OptionsSelection.EnableAppearanceFocusedRow = false;               //禁用正行选中
            //treeList.Appearance.FocusedRow.BackColor = System.Drawing.Color.CadetBlue;      //设置焦点行背景色

            #endregion

            #region 设置TreeList的展开折叠按钮样式和树线样式

            treeList.OptionsView.ShowButtons = true;                  //显示展开折叠按钮
            //treeList.LookAndFeel.UseDefaultLookAndFeel = false;       //禁用默认外观与感觉
            //treeList.LookAndFeel.UseWindowsXPTheme = true;            //使用WindowsXP主题
            treeList.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50;     //设置树线的样式
            treeList.OptionsMenu.ShowExpandCollapseItems = false;

            #endregion

            #region 添加单列

            DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            colNode.Name = String.Format("col{0}", fieldName);
            colNode.Caption = fieldName;
            colNode.FieldName = fieldName;
            colNode.VisibleIndex = 0;
            colNode.Visible = true;

            colNode.OptionsColumn.AllowEdit = false;                        //是否允许编辑
            colNode.OptionsColumn.AllowMove = false;                        //是否允许移动
            colNode.OptionsColumn.AllowMoveToCustomizationForm = false;     //是否允许移动至自定义窗体
            colNode.OptionsColumn.AllowSort = false;                        //是否允许排序
            colNode.OptionsColumn.FixedWidth = false;                       //是否固定列宽
            colNode.OptionsColumn.ReadOnly = true;                          //是否只读
            colNode.OptionsColumn.ShowInCustomizationForm = true;           //移除列后是否允许在自定义窗体中显示

            treeList.Columns.Clear();
            treeList.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode });

            #endregion

            #region 绑定数据源

            treeList.KeyFieldName = keyFieldName;
            treeList.ParentFieldName = parentFieldName;
            treeList.DataSource = data;
            treeList.RefreshDataSource();
            #endregion
        }
    }
}
